<template>
<el-container>
  <el-header style="height: auto;">
    <el-row class="back">
      <el-col>
        <el-button @click="toBack" size="small" type="primary">返回</el-button>
      </el-col>
    </el-row>
    <el-form :model="form" :rules="formRules">
      <el-row>
        <el-col :xl="{ span: 5 }" :lg="{ span: 5 }">
          <el-form-item label="一级流量池" label-width="120px">
            <el-input size="small" v-model="firstpoolName" style="width: 100%;" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="{ span: 5 }" :lg="{ span: 5 }">
          <el-form-item label="理论总量" label-width="120px">
            <el-input size="small" v-model="theoryFlow" style="width: 100%;" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="{ span: 5 }" :lg="{ span: 5 }">
          <el-form-item label="可下分流量" label-width="120px">
            <el-input size="small" v-model="availableFlow" style="width: 100%;" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="{ span: 5 }" :lg="{ span: 7 }">
          <el-form-item class="a" label="业务号码区间查询" label-width="130px" prop="paragraphNumber">
            <el-col :xl="{ span: 6 }" :lg="{ span: 11 }">
              <el-input size="small" style="width: 100%;" v-model="form.startCardNumber"></el-input>
            </el-col>
            <el-col :xl="{ span: 2 }" :lg="{ span: 2 }" style="text-align: center;">-</el-col>
            <el-col :xl="{ span: 6 }" :lg="{ span: 11 }">
              <el-input size="small" style="width: 100%;" v-model="form.endCardNumber"></el-input>
            </el-col>
          </el-form-item>
        </el-col>
        <el-col :xl="{ span: 2 }" :lg="{ span: 2 }">
          <el-form-item label-width="20px">
            <el-button @click="currentPage = 0 ; getSectionCardByCardNumber()" size="small" type="primary">查询</el-button>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <!--el-col(:xl="{ span: 5 }" :lg="{ span: 5 }")
        el-form-item(label="业务号码区间查询" label-width="100px")

        -->
        <!--el-col(:xl="{ span: 5 }" :lg="{ span: 5 }")
        el-form-item(label="起始业务号码" label-width="100px")
          el-input(size="small" v-model="startCardNumber" style="width: 100%;")

        -->
        <!--el-col(:xl="{ span: 5 }" :lg="{ span: 5 }")
        el-form-item(label="结束业务号码" label-width="100px")
          el-input(size="small" v-model="endCardNumber" style="width: 100%;")



        -->
        <el-col>
          <el-button @click="showSecondTotalPool" size="small" type="primary">组成二级流量池</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-header>
  <el-main style="height: auto;">
    <el-table :header-cell-style="{ 'background':$store.state.app.tableBgColor,'color':'#606266' }" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange" v-loading.fullscreen="tableLoading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.2)" :row-key="getRowKeys">
      <el-table-column type="selection" align="center" :reserve-selection="true"></el-table-column>
      <el-table-column prop="cardNumber" label="业务号码" align="center"></el-table-column>
      <el-table-column prop="dictChannelCardStatusName" label="卡状态" align="center"></el-table-column>
      <el-table-column prop="dictCardBodyTypeName" label="卡体类型" align="center"></el-table-column>
    </el-table>
    <div class="block"><span class="demonstration"></span>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-sizes="[50, 100, 200, 400]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :page-count="maxPage" :hide-on-single-page="value" :total="tableData.length" background></el-pagination>
    </div>
  </el-main>
  <el-dialog class="delete" top="33vh" :visible.sync="secondTotalPool" :append-to-body="true" width="30%">
    <el-form :model="form" ref="form" :rules="formRules">
      <el-row>
        <el-col :xl="{ span: 20, push: 2 }">
          <el-form-item label="二级池名称" label-width="110px" prop="flowPoolName">
            <el-input size="small" style="width: 100%;" v-model="form.flowPoolName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form><span class="dialog-footer" slot="footer">
      <el-button @click="secondTotalPool = false" size="small">取消</el-button>
      <el-button type="primary" size="small" @click="saveSecondtotalpoolHandle">确定</el-button></span>
  </el-dialog>
</el-container>
</template>

<script>
import { getSectionCardByCardNumber, editPrimaryFlowPoolList, saveSecondtotalpool } from '@/api/getData'

export default {
  name: 'CardList',
  data() {
    const validateParagraphNumber = (rule, value, callback) => {
      if (this.form.startCardNumber && !this.form.endCardNumber) {
        callback(new Error('尾号段不能为空'))
      } else if (!this.form.startCardNumber && this.form.endCardNumber) {
        callback(new Error('首号段不能为空'))
      } else if (this.form.startCardNumber.length > 50 || this.form.endCardNumber.length > 50) {
        callback(new Error('号段不能超过50位'))
      } else if (this.form.endCardNumber === this.form.startCardNumber && this.form.endCardNumber && this.form.startCardNumber) {
        callback(new Error('号段不能重复'))
      } else {
        callback()
      }
    }
    return {
      form: {
        startCardNumber: '',
        endCardNumber: '',
        flowPoolName:''
      },
      secondTotalPool: false,
      maxPage: 1,
      tableData: [],
      pageSize: 10,
      currentPage: 1,
      ascs: [],
      descs: [],
      multipleSelection: [],
      tableLoading: false,
      cardFlowActUsed: '',
      carFlowSurplus: '',
      value: false,
      cardNumber: '',
      dictChannelRequestStatus: '',
      cumulativeFlow: '',
      cardUse: '',
      firstPoolFlowId: '',
      availableFlow: '',
      startCardNumber: '',
      endCardNumber: '',
      theoryFlow: '',
      firstpoolName: '',
      id: '',
      list: {},
      singleCardMeal: '',
      formRules: {
        paragraphNumber: [
          { validator: validateParagraphNumber, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.firstPoolFlowId = this.$route.params.id
    this.getSectionCardByCardNumber()
    this.editPrimaryFlowPoolList()
  },
  methods: {
    getRowKeys(row) {
      return row
    },
    async editPrimaryFlowPoolList() {
      const returnData = await editPrimaryFlowPoolList({
        id: this.firstPoolFlowId
      })
      if (returnData._result) {
        this.firstpoolName = returnData.data.flowPoolName
        this.theoryFlow = returnData.data.theoryFlow
        this.availableFlow = returnData.data.availableFlow
        this.singleCardMeal = returnData.data.singleCardMeal
      }
    },
    async getSectionCardByCardNumber() {
      const returnData = await getSectionCardByCardNumber({
        firstPoolFlowId: this.firstPoolFlowId,
        cardNumberStart: this.form.startCardNumber,
        cardNumberEnd: this.form.endCardNumber,
        page: {
          current: this.currentPage + '',
          size: this.pageSize + '',
          ascs: this.ascs,
          descs: this.descs
        }
      },
      {
        obj: this,
        key: 'tableLoading'
      })
      this.tableData = []
      if (returnData._result) {
        this.maxPage = Math.ceil(returnData.data.total / this.pageSize) // 分页的最大页数
        // 数据回显
        returnData.data.records.map(item => {
          const obj = {}
          obj.id = item.id
          obj.cardNumber = item.cardNumber //业务号码
          obj.dictMyCardStatus = item.dictMyCardStatus // 卡状态
          obj.dictCardTypeName = item.dictCardTypeName
          obj.dictCardBodyTypeName = item.dictCardBodyTypeName
          obj.dictChannelCardStatusName = item.dictChannelCardStatusName
          obj.dictChannelCardStatus = item.dictChannelCardStatus
          obj.cardFlowImaginary = item.cardFlowImaginary
          this.tableData.push(obj)
        })
        this.tableLoading = false
      }
    },
    // 显示组成二级流量池提示弹窗
    showSecondTotalPool() {
      if(this.multipleSelection.length == 0){
        this.$message({ message: '请选择准备组池的卡', type: 'error' })
        return
      }
      this.secondTotalPool = true
    },
    // 提交二级流量池
    async saveSecondtotalpoolHandle() {
      if(this.form.flowPoolName == null || this.form.flowPoolName == ''){
        this.$message({message:'请输入二级池名称', type:'error'})
      }
      const returnData = await saveSecondtotalpool({
        firstPoolId: this.firstPoolFlowId,
        singleCardMeal: this.singleCardMeal,
        flowPoolName: this.form.flowPoolName,
        cardNumber: 0,
        list: this.multipleSelection.map(item => {
          return { id: item.id , dictChannelCardStatus:item.dictChannelCardStatus}
        })
      })
      if (returnData._result) {
        this.secondTotalPool = false
        this.$message({ message: '二级流量池设置成功', type: 'success' })
        this.getSectionCardByCardNumber()
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      //console.log(`每页 ${val} 条`)
      this.pageSize = val
    },
    handleCurrentChange(val) {
      //console.log(`当前页: ${val}`)
      this.currentPage = val
      this.initData()
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    toUsage() {
      this.$router.push({ path: '/package/usage' })
    },
    toBack() {
      this.$router.back(-1)
    }
  }
}
</script>

<style scoped lang="scss">
  .el-container {
    .el-header {
      margin-top: 5vh;
      .back {
        margin-bottom: 3vh;
      }
    }
    .el-main {
      .block {
        float: right;
      }
    }
  }
</style>
